package controler;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import view.IListView;
import ema.tpjee.banking.dao.SessionFacade;
import ema.tpjee.banking.model.Customer;



public class CustomerControler  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6485014212164711258L;
	//private BankControler bankControler;
	//private Customer currentCustomer;
	
	//private int id=100;
	private SessionFacade sessionFacade;

	
	public CustomerControler() {
		super();
		System.out.println("creating CustomerControler");
	}

	public SessionFacade getSessionFacade() {
		return sessionFacade;
	}

	public void setSessionFacade(SessionFacade sessionFacade) {
		this.sessionFacade = sessionFacade;
		System.out.println(this.getClass().getName()+" setSessionFacade");
	}

/*

	private Customer getCustomerById(String id) {
		int customerid = Integer.parseInt(id);
		for (Iterator<Customer> i = getList().iterator(); i.hasNext();) {
			Customer cust = i.next();
			if (cust.getId() == customerid)
				return cust;
		}
		return null;
	}*/

	public List<Customer> getList() {
		return sessionFacade.getCustomers();
	}
/*
	private void createAdd() {
		Customer c = new Customer(id++, "nomane", "noformane", "noaddress",
				"nozip");
		getList().add(c);
		sessionFacade.setCurrentCustomer(c);
	}
	*/
/*
	private void dodelete(Customer customer) {
		for (Iterator i = getList().iterator(); i.hasNext();) {
			Customer cust = (Customer) i.next();
			if (customer == cust)
				i.remove();
		}
	}
	*/

	public void add(Customer customer) {
		getList().add(customer);
	}


	public String cancel() {
		System.out.println("action "+this.getClass().getName()+" cancel()");
		return "list";
	}

	public String create() {
		System.out.println("action "+this.getClass().getName()+" create()");
		//createAdd();
		sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().create());
		return "edit";
	}

	public String edit() {
		System.out.println("action "+this.getClass().getName()+" edit()");
		//int customerId = Integer.parseInt(sessionFacade.getCustomerView().getIdCmdValue());
		int customerId_ = sessionFacade.getCurrentCustomer().getId();//Integer.parseInt(sessionFacade.getCustomerView().getIdCmdValue());
		System.out.println(customerId_);
		//sessionFacade.setCurrentCustomer(getCustomerById(customerId));
		//sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().find(customerId));
		return "edit";
	}
	
	//binding="#{sessionFacade.customerView.idCmd}"
	//	<f:setPropertyActionListener value="#{vCustomer}" target="#{sessionFacade.currentCustomer}" />
	
	public String save() {
		System.out.println("action "+this.getClass().getName()+" save()");
		return "list";
	}
/*
	private Customer nextCustomer(Customer c) {
		for (Iterator<Customer> i = getList().iterator(); i.hasNext();) {
			Customer cust = i.next();
			if (cust.getId() == c.getId())
			try {
				return i.next();
			} catch (Exception e) {
				return null;
			}			
		}
		return null;
	}
*/
	public String next() {
		System.out.println("action "+this.getClass().getName()+" next()");
		sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().next((sessionFacade.getCurrentCustomer())));
		return "edit";
	}
	
	public String prior() {
		System.out.println("action "+this.getClass().getName()+" prior()");
		//sessionFacade.setCurrentCustomer(nextCustomer(sessionFacade.getCurrentCustomer()));
		sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().prior((sessionFacade.getCurrentCustomer())));
		return "edit";
	}
	public String copy() {
		System.out.println("action "+this.getClass().getName()+" copy()");
		sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().clone((sessionFacade.getCurrentCustomer())));
		return "list";
	}
	
	public String foobar() {
		System.out.println("action "+this.getClass().getName()+" foobar()");
		return "edit";
	}
	
	
	public String delete() {
		System.out.println("action "+this.getClass().getName()+" delete()");
	   /*
		Customer prior = sessionFacade.getCustomerDao().prior(sessionFacade.getCurrentCustomer());
		dodelete(sessionFacade.getCurrentCustomer());
		sessionFacade.setCurrentCustomer(prior);*/
		sessionFacade.setCurrentCustomer(sessionFacade.getCustomerDao().delete((sessionFacade.getCurrentCustomer())));
		return "list";
	}


	public void setView(IListView view) {
		//this.view = view;
	}


}
